package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.core.Clazz;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.NotifyMessage;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by j_hu on 5/10/17.
 */

public class SyncClassHandler extends SyncMetaCommandRespHandler {
    public SyncClassHandler(SyncMetaCommand cmd) {
        super("class.sync", cmd);
    }

    @Override
    public boolean handle(InputStream input) {
        boolean handleOk = false;
        try {
            Gson gson = new Gson();
            SyncMetaCommandRS<SyncClassListRS> syncRS = (SyncMetaCommandRS<SyncClassListRS>)gson.fromJson(
                    getString(input),
                    new TypeToken<SyncMetaCommandRS<SyncClassListRS>>(){}.getType());

            if (syncRS != null && syncRS.getData() != null) {
                getCommand().notify(NotifyMessage.info(
                        String.format("%s<%s>", Consts.INFO_SYNC_CLASS, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));

                ArrayList<Clazz> list = new ArrayList<>();
                for(ClassRS c:syncRS.getData().getList()) {
                    Clazz clazz = new Clazz(this.getCommand().getDbHelper());
                    clazz.setNo(c.getNo());
                    clazz.setName(c.getName());
                    clazz.setGradeNo(c.getGradeNo());
                    clazz.setDeleted(c.isDeleted());
                    list.add(clazz);
                }
                if (getCommand().getDbHelper().groupAddClazz(list)) {
                    getCommand().getDbHelper().getSyncRecord().update(getName(), syncRS.getLastMetaUpdateTime());
                    getCommand().notify(NotifyMessage.info(
                            String.format("%s<%s>", Consts.INFO_SYNC_CLASS_OK, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                    handleOk = true;
                } else {
                    getCommand().notify(NotifyMessage.info(
                            String.format("%s<%s>", Consts.INFO_SYNC_CLASS_FAIL, syncRS.getData().getList().size(), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST)));
                }
            }
        }catch(Exception e) {
            getCommand().raiseError(e);
        }
        return handleOk;
    }

    public class SyncClassListRS {
        private List<ClassRS> list;

        public List<ClassRS> getList() {
            return list;
        }
    }

    public class ClassRS {
        private int no;
        private String name;
        private boolean deleted;

        @SerializedName("grade_no")
        private int gradeNo;

        public int getNo() {
            return no;
        }

        public int getGradeNo() {
            return gradeNo;
        }

        public String getName() {
            return name;
        }

        public boolean isDeleted() {
            return deleted;
        }
    }
}
